﻿using System.Diagnostics;
using RaptorEngine.Components.CollidableComponent;
using RaptorEngine;
using RaptorEngine.Components;
using Microsoft.Xna.Framework;

namespace OilRigSumoWrestlers.Actions
{
    class PushAction : CollisionAction
    {
        /// <summary>
        /// Handles collision between two boundingSpheres with newton physics solid spheres and no energy wasted. 
        /// </summary>
        /// <param name="ownEntity">The parent entity of the action</param>
        /// <param name="ownVolume">The parent bounding volume of the action</param>
        /// <param name="othersEntity">The other colliding entity</param>
        /// <param name="othersVolume">The boundingVolume of the other entity</param>
        /// <param name="gameTime"></param>
        public override void HandleCollision(Entity ownEntity, BoundingVolume ownVolume, Entity othersEntity,
            BoundingVolume othersVolume)
		{
			Movable othersMovable = null;
            if (othersEntity.HasComponent(ComponentType.Movable))
            {
                //GameEngine.Instance.Audio.PlaySound("push_hit", 1);
                GameEngine.Instance.Audio.Play3DSound("push_hit", 1f, othersEntity.Position);
				othersMovable = (Movable)othersEntity.GetComponent(ComponentType.Movable);
                Vector2 pushDir = new Vector2(
                    othersVolume.Center.X - ownVolume.Center.X,
                    othersVolume.Center.Z - ownVolume.Center.Z);
				
                pushDir.Normalize();
				othersMovable.Velocity = new Vector3(
					othersMovable.Velocity.X + pushDir.X * GameConstant.PUSH_FORCE,
					othersMovable.Velocity.Y,
					othersMovable.Velocity.Z + pushDir.Y * GameConstant.PUSH_FORCE);

				if (othersMovable.Velocity.Length() > GameConstant.MAX_VELOCITY_AFTER_PUSH)
				{
					othersMovable.Velocity = Vector3.Normalize(othersMovable.Velocity) * GameConstant.MAX_VELOCITY_AFTER_PUSH;
				}

	            if (ownEntity.HasComponent(ComponentType.Movable))
	            {

					Movable ownMovable = (Movable)ownEntity.GetComponent(ComponentType.Movable);
					ownMovable.Velocity = new Vector3(
			        ownMovable.Velocity.X - pushDir.X * GameConstant.PUSHERS_FORCE,
			        ownMovable.Velocity.Y,
					ownMovable.Velocity.Z - pushDir.Y * GameConstant.PUSHERS_FORCE);
	            }
            }
        }
    }
}
